package Model;


import JDBC.Constants;
import JDBC.DBConnection;


import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class Evento {
	

	public void adicionarEvento(String idEstabelecimento, String TipoEvento, Date data,String DescricaoEvento){

		DBConnection DBConnection = new DBConnection(Constants.DB_NAME, Constants.USER, Constants.PASSWORD, false, null);
        DBConnection.ConnectionHandler ch1 = DBConnection.getConnectionHandlerInstance();
        DBConnection.ConnectionHandler ch2 = DBConnection.getConnectionHandlerInstance();

        String insert1 = "insert into tipoDeEvento (tipoDoEvento, descricao) values ((select count(tipoDoEvento) + 1 from tipoDeEvento) , '"+DescricaoEvento+"')";
        String insert2 = "insert into eventoOferecido (idEstabelecimento, tipoDoEvento) values ("+Integer.parseInt(idEstabelecimento)+", (SELECT TOP 1 tp.tipoDoEvento FROM TipoDeEvento as tp ORDER BY tp.tipoDoEvento DESC))";

        System.out.println("query1: " + insert1);
        System.out.println("query2: " + insert2);
        DBConnection.returnResultSetSelectStatement(insert1, 1, ch1);
        DBConnection.returnResultSetSelectStatement(insert2, 1, ch2);
        DBConnection.setCommit(ch1);
        DBConnection.setCommit(ch2);

        DBConnection.closeDatabase();

	}

    // Modificado -- acrescentado
    public static String[] obterTiposEventos() {
        DBConnection dbConnection = new DBConnection(Constants.DB_NAME, Constants.USER, Constants.PASSWORD, false, null);

        JDBC.DBConnection.ConnectionHandler ch = DBConnection.getConnectionHandlerInstance();
        String sql = "SELECT * FROM tipoDeEvento";
        dbConnection.returnResultSetSelectStatement(sql, 1, ch);

        List<String> tiposEventos = new ArrayList<String>();
        try {
            while(ch.r.next()) {
                String tipoEvento = ch.r.getString("descricao");
                tiposEventos.add(tipoEvento);
            }
            ch.r.close();
        } catch (SQLException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        dbConnection.closeDatabase();

        return tiposEventos.toArray(new String[0]);
    }
	
	
}
